<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('base_mapper.php');
Class Vendor_mapper extends Base_mapper{
	protected $object_class = 'vendor_object';
	
	protected $table = 'vendors';
	
	protected $primary_key = 'id';
	
	protected function doBuildObject($row){
		return new Vendor_object($row->id, $row->name, $row->vendor_code, $row->business_type, $row->primary_branch_id);
	}
	public function count(){
		return $this->db->count_all($this->table);
	}
	protected function doDelete(Vendor_object $obj){
		//delete all the branches
		foreach($obj->getBranches as $branch){
			$branch->delete();
		}
		//delete all the coupons
		foreach($obj->getCoupons() as $coupons){
			$coupons->delete();
		}
		//delete the relationship 
		$tables = array('vendors_has_contacts');
		$this->db->where('vendors_id', $obj->getId());
		$this->db->delete($tables);
		parent::doDelete($obj);
	}
	protected function doInsert($obj){
		$data = array(
				'name' => $obj->getVendorName() ,
				'vendor_code' => $obj->getVendorCode() ,
				'business_type' => $obj->getBusinessType()
		);
		$this->db->insert($this->table, $data);
		parent::doInsert($obj);
	}
	protected function doUpdate($obj){
		$data = array(
				'name' => $obj->getVendorName() ,
				'vendor_code' => $obj->getVendorCode() ,
				'business_type' => $obj->getBusinessType()
		);
		$this->db->where('id', $obj->getId());
		$this->db->update($this->table, $data);
		parent::doUpdate($obj);
	}
}